<template>
  <div class="userManager-box">
    <el-row class="btn-group">
      <el-col :offset="2" :span="4"><el-button type="primary" @click="findRole">新增</el-button></el-col>
      <el-col :span="4"><el-button type="danger" @click="bathdeleteUser">批量删除</el-button></el-col>
      <el-col :span="9"><el-input v-model="name" prefix-icon="el-icon-search" placeholder="请输入搜索的用户"></el-input><el-button type="primary" @click="reachUser">搜索</el-button></el-col>
      <el-col :span="3">
        <el-button-group>
        <el-button type="primary">升序</el-button>
        <el-button type="primary">降序</el-button>
        </el-button-group>
      </el-col>
    </el-row>
    <el-row class="userManager-content">
      <el-table :data="user" @selection-change="getIds">
        <el-table-column type="selection" label="全选"></el-table-column>
        <el-table-column label="id" width="40px" prop="user_id"></el-table-column>
        <el-table-column label="用户名" prop="name"></el-table-column>
        <el-table-column label="账号" prop="account"></el-table-column>
        <el-table-column label="手机号码" prop="phone"></el-table-column>
        <el-table-column label="性别">
          <template slot-scope="scope">
            <span>{{sexArray[scope.row.sex]}}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="state">
          <template slot-scope="scope">
            <el-switch v-model="user[scope.$index].state" active-text="未封禁" inactive-text="封禁" :active-value="0" :inactive-value="1" @click.native="tiggerState(scope.row.user_id,scope.row.state)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="findOldUser(scope.row.user_id)">修改</el-button>
            <el-button type="text" @click="deletedUser(scope.row.user_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row type="flex" justify="center">
      <el-pagination
            @size-change="pageSizeChange"
            @current-change="currPageChange"
            :current-page="curr_page"
            :page-sizes="[10, 50, 100, 500, 1000]"
            :page-size="page_size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
    </el-row>
    <el-row>
      <!-- 用户新增 -->
      <el-dialog title="用户新增" :visible.sync="isShow" width="35%">
        <div class="addForm">
        <el-form ref="addUser" :model="params" :rules="rules" resetFields class="addform">
          <el-form-item label="账号" prop="account">
            <el-input placeholder="请输入账号" v-model="params.account"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input placeholder="请输入密码" v-model="params.password"></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input placeholder="请输入姓名" v-model="params.name"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input placeholder="请输入手机号" v-model="params.phone"></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-radio-group v-model="params.sex">
              <el-radio :label="0">保密</el-radio>
              <el-radio :label="1">男</el-radio>
              <el-radio :label="2">女</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <center>
            <el-button type="primary" @click="addUsers('addUser')">提交</el-button>
            <el-button type="primary">重置</el-button>
            </center>
          </el-form-item>
        </el-form>
        </div>
      </el-dialog>
      </el-row>
      <el-row>
        <!-- 用户新增抽屉 -->
        <el-dialog title="用户修改" :visible.sync="isShow1" width="35%">
          <div class="addForm">
          <el-form ref="updateUser" :model="params" :rules="rules" resetFields class="addform">
            <el-form-item label="账号" prop="account">
              <el-input placeholder="请输入账号" v-model="params.account"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input placeholder="请输入密码" v-model="params.password"></el-input>
            </el-form-item>
            <el-form-item label="姓名" prop="name">
              <el-input placeholder="请输入姓名" v-model="params.name"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
              <el-input placeholder="请输入手机号" v-model="params.phone"></el-input>
            </el-form-item>
            <el-form-item label="性别">
              <el-radio-group v-model="params.sex">
                <el-radio :label="0">保密</el-radio>
                <el-radio :label="1">男</el-radio>
                <el-radio :label="2">女</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="updateUser('updateUser')">提交</el-button>
              <el-button type="primary">重置</el-button>
            </el-form-item>
          </el-form>
          </div>
        </el-dialog>
        </el-row>
  </div>
</template>
<script>
import http from '@/utils/HttpUtil'
export default {
  data () {
    let checkPhone = (rule, value, callback) => {
      let phoneReg = /^1[3456789]\d{9}$/
      if (!phoneReg.test(value)) {
        return callback(new Error('手机号格式不正确!'))
      }
      callback()
    }
    return {
      sexArray: ['保密', '男', '女'],
      total: 0,
      page_size: 10,
      curr_page: 1,
      isShow: false,
      isShow1: false,
      name: '',
      user: [],
      ids: '',
      params: {
        user_id: null,
        account: '',
        name: '',
        password: '',
        phone: '',
        state: 0,
        sex: 0,
        version: null
      },
      action: {
        selectUser: 'shoes_Web_exploded/User/selectPageByName',
        addUser: 'shoes_Web_exploded/User/addUser',
        deleted: 'shoes_Web_exploded/User/deletedUser',
        updateUsers: 'shoes_Web_exploded/User/updateUser',
        selectUserByid: 'shoes_Web_exploded/User/selectUserById',
        updateUserState: 'shoes_Web_exploded/User/updateUserState'
      },
      rules: {
        account: [
          {
            required: true, message: '账号不能为空', trigger: 'blur'
          },
          {
            validator: checkPhone, trigger: 'blur'
          }],
        name: [
          {
            required: true, message: '姓名不能为空', trigger: 'blur'
          }],
        password: [
          {
            required: true, message: '密码不能为空', trigger: 'blur'
          }],
        phone: [
          {
            required: true, message: '手机号不能为空', trigger: 'blur'
          },
          {
            validator: checkPhone, trigger: 'blur'
          }]
      }
    }
  },
  components: {

  },
  computed: {

  },
  methods: {
    // 修改用户状态
    tiggerState (id, state) {
      let params = {
        'state': state
      }
      http.xhrPost(this, this.action.updateUserState + '/' + id, params, (res) => {
        this.$message({
          type: 'success',
          message: res.message
        })
      })
    },
    // 用户回显数据
    findOldUser (id) {
      this.isShow1 = true
      http.xhrPost(this, this.action.selectUserByid + '/' + id, null, (res) => {
        this.params.account = res.data.account
        this.params.password = res.data.password
        this.params.phone = res.data.phone
        this.params.name = res.data.name
        this.params.state = res.data.state
        this.params.sex = res.data.sex
        this.params.version = res.data.version
        this.params.user_id = res.data.user_id
      })
    },
    findRole () {
      this.isShow = true
      this.params = {
        user_id: null,
        account: '',
        name: '',
        password: '',
        phone: '',
        sex: 0,
        version: null
      }
    },
    reachUser () {
      this.getUserList(this.name, 1, this.pageSize)
    },
    pageSizeChange (pageSize) {
      this.getUserList(this.name, 1, pageSize)
    },
    currPageChange (currPage) {
      this.getUserList(this.name, currPage, this.page_size)
    },
    getUserList (name = null, curr_page = 1, page_size = 10) {
      const params = {
        'curr_page': curr_page,
        'page_size': page_size
      }
      if (name !== null && name !== '') {
        params['name'] = name
      }
      http.xhrPost(this, this.action.selectUser, params, (res) => {
        console.log(res)
        this.user = res.data.list
        this.page_size = res.data.pageSize
        this.curr_page = res.data.pageNum
        this.total = res.data.total
      })
    },
    addUsers (refName) {
      this.$refs[refName].validate((valid) => {
        let params = this.params
        http.xhrPost(this, this.action.addUser, params, (res) => {
          this.$message({
            type: 'success',
            message: res.message
          })
          this.isShow = false
          this.getUserList()
          this.params = {
            user_id: null,
            account: '',
            name: '',
            password: '',
            phone: '',
            sex: 0,
            version: null
          }
        })
      })
    },
    deletedUser (id) {
      this.$confirm('您正在执行删除操作', '提示', {
        confirmButtonText: '残忍丢弃',
        cancelButtonText: '容我想想',
        type: 'warning'
      }).then(() => {
        http.xhrPost(this, this.action.deleted + '/' + id, null, (res) => {
          this.$message({
            type: 'success',
            message: res.message
          })
        })
      })
    },
    getIds (user) {
      // 清楚上次选中的id
      this.ids = ''
      user.forEach((item, index) => {
        this.ids += item.user_id + ','
      })
    },
    bathdeleteUser () {
      if (this.ids !== '') {
        this.$confirm('您正在执行批量删除操作', '提示', {
          confirmButtonText: '残忍丢弃',
          cancelButtonText: '容我想想',
          type: 'warning'
        }).then(() => {
          let Ids = this.ids.substring(0, this.ids.length - 1)
          this.deleted(Ids)
        })
      } else {
        this.$message({
          type: 'error',
          message: '请至少勾选一条数据'
        })
      }
    },
    deleted (Ids) {
      http.xhrPost(this, this.action.deleted + '/' + Ids, null, (res) => {
        this.$message({
          type: 'success',
          message: res.message
        })
        this.getUserList(this.name, this.curr_page, this.page_size)
      }, 'delete')
    },
    updateUser (refName) {
      this.$refs[refName].validate((valid) => {
        if (valid) {
          let params = this.params
          console.log(params)
          http.xhrPost(this, this.action.updateUsers, params, (res) => {
            this.$message({
              type: 'success',
              message: res.message
            })
            this.params = {
              user_id: null,
              account: '',
              name: '',
              password: '',
              phone: '',
              sex: 0,
              version: null
            }
            this.isShow1 = false
            this.getUserList(this.name, this.curr_page, this.page_size)
          })
        }
      })
    }
  },
  mounted () {
    this.getUserList()
  }
}
</script>

<style lang="scss">
@import '../../assets/style/common.scss';
  .userManager-box {
    margin: 0px;
    padding: 0px;
    .btn-group {
      .el-input {
        width: 250px;
      }
    }
    .userManager-content {
      margin-top: 20px;
    }
    .addForm {

    }
  }
</style>
